package fulbitofacil.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import fulbitofacil.exception.DAOExcepcion;
import fulbitofacil.modelo.Documento;
import fulbitofacil.util.ConexionBD;

public class DocumentoDAO extends BaseDAO {

	public Collection<Documento> listarDocumentos() throws DAOExcepcion {
		String query = "select idDocumento, descripcion, pais from documento";
		Collection<Documento> c = new ArrayList<Documento>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Documento doc = new Documento();
				doc.setId_documento(rs.getInt("idDocumento"));
				doc.setDescripcion(rs.getString("descripcion"));
				doc.setPais(rs.getString("pais"));
				c.add(doc);
			}

			return c;

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}

	}
}
